package server.view;

import java.awt.EventQueue;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JMenu;
import javax.swing.JButton;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;

import server.ppt.PptLauncher;
import server.ppt.server.FileServer;
import server.server.AndroidServer;
import server.toolkit.SystemUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

public class MainFrame implements DropHandler {

	private JFrame frame;
	private DroppableTextArea dta;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame window = new MainFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		new FileServer().startService(8080, SystemUtil.getLargestDisk()
				.getAbsolutePath() + File.separator + "sliande");
		new AndroidServer().start();
	}

	/**
	 * Create the application.
	 */
	public MainFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setTitle("@@王明亮制造@@");
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);

		JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(0, 0, 434, 21);
		frame.getContentPane().add(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem mntmOpen = new JMenuItem("Open");
		mntmOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openActionPerformed(e);
			}
		});
		mntmOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
				InputEvent.CTRL_MASK));
		mnFile.add(mntmOpen);

		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exitActionPerformed(e);
			}
		});
		mntmExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
				InputEvent.CTRL_MASK));
		mnFile.add(mntmExit);

		JButton btnOpen = new JButton("Open");
		btnOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openActionPerformed(e);
			}
		});
		btnOpen.setBounds(21, 208, 169, 44);
		frame.getContentPane().add(btnOpen);

		JButton btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				exitActionPerformed(e);
			}
		});
		btnExit.setBounds(244, 208, 169, 44);
		frame.getContentPane().add(btnExit);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(21, 41, 392, 157);
		frame.getContentPane().add(scrollPane);

		dta = new DroppableTextArea();
		dta.setColumns(20);
		dta.setRows(5);
		dta.setText("drop your ppt file here");
		dta.setHandler(this);
		scrollPane.setViewportView(dta);
	}

	private void openActionPerformed(ActionEvent evt) {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setFileFilter(new MyCustomFilter());
		int returnVal = fileChooser.showOpenDialog(this.frame.getContentPane());
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			this.handle(file);
		} else {
			System.out.println("File access cancelled by user.");
		}
	}

	private void exitActionPerformed(ActionEvent evt) {
		System.exit(0);
	}

	@Override
	public void handle(final File file) {
		this.dta.setText(file.getAbsolutePath());
		try {
			PptLauncher launcher = new PptLauncher(file.getAbsolutePath(),
					false);
			String path = SystemUtil.getLargestDisk().getAbsolutePath();
			launcher.startCapture(path + File.separator + "test", path
					+ File.separator + "sliande");
			launcher.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class MyCustomFilter extends FileFilter {
		@Override
		public boolean accept(File file) {
			return file.isDirectory()
					|| file.getAbsolutePath().endsWith(".ppt")
					|| file.getAbsolutePath().endsWith(".pptx");
		}

		@Override
		public String getDescription() {
			return "ppt documents (*.ppt;*.pptx)";
		}
	}
}
